package com.myjava.managesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myjava.managesystem.common.R;
import com.myjava.managesystem.entity.Employee;
import com.myjava.managesystem.mapper.EmployeeMapper;
import com.myjava.managesystem.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Service
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public R<Employee> empLogin(HttpServletRequest request, String username, String password) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,username);
        Employee emp = this.getOne(wrapper);
        if(emp == null){
            return R.error("用户不存在");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        else if(!emp.getStatus().equals(1)){
            return R.error("该账号已被禁用");
        }
        else{
            request.getSession().setAttribute("employee",emp.getId());
            return R.success(emp);
        }
    }

}
